﻿from google.appengine.ext import db
import datetime
import hashlib,urllib
from google.appengine.api import users
from app.functions.funcs import render,split_tags,formate_time,gen_duration
import xml.etree.ElementTree as et
from google.appengine.api import datastore_types
from django.utils import simplejson

class JSONProperty(db.Property):
    def get_value_for_datastore(self, model_instance):
        value = super(JSONProperty, self).get_value_for_datastore(model_instance)
        return self._deflate(value)
    def validate(self, value):
        return self._inflate(value)
    def make_value_from_datastore(self, value):
        return self._inflate(value)
    def _inflate(self, value):
        if value is None:
            return {}
        if isinstance(value, unicode) or isinstance(value, str):
            return simplejson.loads(value)
        return value
    def _deflate(self, value):
        return simplejson.dumps(value)
    data_type = datastore_types.Text

class Card(db.Model):
    title = db.StringProperty(required=True)
    identity = db.StringProperty(required=True)
    number = db.StringProperty()
    category = db.StringProperty()
    quantity = db.StringProperty()
    created_on = db.DateTimeProperty(auto_now_add=True)

    def get_id(self):
        return '%s' %self.key().id()

class UserItems(db.Model):
    data = JSONProperty()
    created_on = db.DateTimeProperty(auto_now_add=True)
    updated_on = db.DateTimeProperty(auto_now=True)
    owner = db.UserProperty()

    def get_id(self):
        return '%s' %self.key().id()

class UserInfo(db.Model):
    charactor = db.StringProperty(required=True)
    realm = db.StringProperty(required=True)
    region = db.StringProperty(required=True)
    email = db.EmailProperty(required=True)
    created_on = db.DateTimeProperty(auto_now_add=True)

    def get_id(self):
        return '%s' %self.key().id()